package service.calculator.bagnetified.sanmateo;

import java.math.BigDecimal;

import models.Payment;
import models.Rate;
import service.AbstractPayrollCalculator;
import util.Rounder;

public class BagnetifiedSanMateoPayrollCalculatorImpl extends AbstractPayrollCalculator {

	public BigDecimal calculatePayment(Rate rate, Payment payment) {
        BigDecimal paymentCalculation = null;
        BagnetifiedSanMateoPaymentType paymentType = BagnetifiedSanMateoPaymentType
                .valueOf(payment.paymentType);
        final BigDecimal paymentHrs = BigDecimal.valueOf(payment.hours);
        final BigDecimal rateValue = rate.value;

        final BigDecimal ctpaRate = BigDecimal.valueOf(1.5625);
        final BigDecimal seaRate = BigDecimal.valueOf(1.625);

        switch (paymentType) {
        case BASIC:
        case ADJ_RT:
        	paymentCalculation = rate.value.multiply(paymentHrs);
            break;
        case HP:
        case HPU:
        case LHRT:
        case ADJ_LHRT:
        	paymentCalculation = rateValue.add(ctpaRate).add(seaRate).multiply(paymentHrs);
            break;
        case OT:
        case ADJ_OT:
            BigDecimal otRate = rate.value.add(rate.value.multiply(BigDecimal
                    .valueOf(0.25)));
            paymentCalculation = otRate.multiply(paymentHrs);
            break;
        case LHOT:
        case ADJ_LHOT:
            // (BasicRate x 2) + ((BasicRate x 2) x 30%)
        	BigDecimal lhotRate = rate.value.add(rate.value.multiply(BigDecimal.valueOf(1)));
            lhotRate = lhotRate.add(lhotRate.multiply(BigDecimal.valueOf(0.30)));
            paymentCalculation = lhotRate.multiply(paymentHrs);
            break;
        case SHRT:
        case ADJ_SHRT:
        	// Only 30% of the rate
            BigDecimal shrtRate = rate.value.multiply(BigDecimal.valueOf(0.3));
            paymentCalculation = shrtRate.multiply(paymentHrs);
            break;
        case SUNDAY:
        	// 30% of the rate + ecola rate
            BigDecimal sundayRate = rate.value.multiply(BigDecimal.valueOf(0.3));
            paymentCalculation = sundayRate.multiply(paymentHrs);
            break;
        case SHOT:
        case ADJ_SHOT:
        case RDOT:
        case ADJ_RDOT:
            // (BasicRate x 1.3) + ((BasicRate x 1.3) x 30%)
            BigDecimal shotRate = rate.value.multiply(BigDecimal.valueOf(1.3));
            shotRate = shotRate.add(shotRate.multiply(BigDecimal.valueOf(0.30)));
            paymentCalculation = shotRate.multiply(paymentHrs);
            break;
        case RDRT:
        case ADJ_RDRT:
            // Plus 30%
            BigDecimal rdrtRate = rate.value.add(rate.value.multiply(BigDecimal
                    .valueOf(0.3)));
            paymentCalculation = rdrtRate.multiply(paymentHrs);
            break;
        case RDLH:
        	// Plus 160%
            BigDecimal rdlhRate = rate.value.add(rate.value.multiply(BigDecimal
                    .valueOf(1.6)));
            paymentCalculation = rdlhRate.multiply(paymentHrs);
            break;
        case RDSH:
            // Plus 60%
            BigDecimal rdshRate = rate.value.add(rate.value.multiply(BigDecimal
                    .valueOf(0.6)));
            paymentCalculation = rdshRate.multiply(paymentHrs);
            break;
        case NDRT:
        case ADJ_NDRT:
            // Only 10.01% of the rate
            BigDecimal ndrtRate = rate.value.multiply(BigDecimal.valueOf(0.1001));
            paymentCalculation = ndrtRate.multiply(paymentHrs);
            break;
        case NDLH:
            // Only 20% of the rate
            BigDecimal ndlhRate = rate.value.multiply(BigDecimal.valueOf(0.2));
            paymentCalculation = ndlhRate.multiply(paymentHrs);
            break;
        case NDSH:
        	// Only 13% of the rate (10% of rate bec. of ND plus 3% bec. of SH)
            BigDecimal ndshRate = rate.value.multiply(BigDecimal.valueOf(0.13));
            paymentCalculation = ndshRate.multiply(paymentHrs);
            break;
        case ADJ:
        case SVC_CHRG:
            // This is money value already
            paymentCalculation = paymentHrs;
            break;
        case CTPA:
        	paymentCalculation = ctpaRate.multiply(paymentHrs);
        	paymentCalculation = Rounder.roundup(paymentCalculation);
        	break;
        case SEA:
            paymentCalculation = seaRate.multiply(paymentHrs);
            break;
        default:
            System.out.println("WARNING: " + payment.paymentType
                    + " payment type not yet supported.");
        }

        if (paymentCalculation != null) {
        	paymentCalculation = Rounder.roundup(paymentCalculation);
        }

        return paymentCalculation;
    }

}
